﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.EnterpriseServices;
using System.Text;
using System.Linq.Expressions;


namespace MySkiPatrolSite
{
    public static class HtmlHelpers
    {
        //public static MvcHtmlString HiddenJsonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        //{
        //    return HiddenJsonFor(htmlHelper, expression, (IDictionary<string, object>)null);
        //}

        //public static MvcHtmlString HiddenJsonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
        //{
        //    return HiddenJsonFor(htmlHelper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        //}

        //public static MvcHtmlString HiddenJsonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
        //{
        //    var name = ExpressionHelper.GetExpressionText(expression);
        //    var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

        //    var tagBuilder = new TagBuilder("input");
        //    tagBuilder.MergeAttributes(htmlAttributes);
        //    tagBuilder.MergeAttribute("name", name);
        //    tagBuilder.MergeAttribute("type", "hidden");

        //    var json = JsonConvert.SerializeObject(metadata.Model);

        //    tagBuilder.MergeAttribute("value", json);

        //    return MvcHtmlString.Create(tagBuilder.ToString());
        //}

        public static MvcHtmlString ActionImage(this HtmlHelper html, string action, object routeValues, string imagePath, string alt, string idName)
        {
            var url = new UrlHelper(html.ViewContext.RequestContext);
            // build the <img> tag
            var imgBuilder = new TagBuilder("img");
            imgBuilder.MergeAttribute("src", url.Content(imagePath));
            imgBuilder.MergeAttribute("alt", alt);
            imgBuilder.MergeAttribute("title", alt);
            imgBuilder.MergeAttribute("border", "0");
            string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);
            // build the <a> tag
            var anchorBuilder = new TagBuilder("a");
            anchorBuilder.MergeAttribute("href", url.Action(action, routeValues));
            anchorBuilder.MergeAttribute("alt", alt);
            anchorBuilder.MergeAttribute("title", alt);
            anchorBuilder.MergeAttribute("id", idName);
            anchorBuilder.InnerHtml = imgHtml; // include the <img> tag inside
            string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);
            return MvcHtmlString.Create(anchorHtml);
        }

        public static MvcHtmlString Mailto(this HtmlHelper helper, string emailAddress, string displayText = null, string subjectText = null, string bodyText = null)
        {
            bool hasParms = false;

            if (string.IsNullOrEmpty(displayText))
                displayText = emailAddress;

            if (!string.IsNullOrEmpty(subjectText))
            {
                subjectText = "?subject=" + subjectText;
                hasParms = true;
            }

            string body = string.Empty;
            if (!string.IsNullOrEmpty(bodyText))
            {
                if (hasParms)
                {
                    body = "&";
                }
                else
                {
                    body = "?";
                }
                body += "body=" + bodyText;
            }

            var sb = string.Format("<a href=\"{0}{1}{3}{4}\" title=\"{1}\">{2}</a>", CharEncode("mailto:"), CharEncode(emailAddress), CharEncode(displayText), CharEncode(subjectText), CharEncode(body));
            return MvcHtmlString.Create(sb);
        }

        public static string CharEncode(string value)
        {
            var retval = "";
            if (!string.IsNullOrEmpty(value))
            {
                var enc = Encoding.Default;

                for (var i = 0; i < value.Length; i++)
                {
                    retval += "&#" + enc.GetBytes(new[] { Convert.ToChar(value.Substring(i, 1)) })[0] + ";";
                }
            }
            return retval;
        }
    }
}